package pl.polsl.mmm.projectcomm.programs;

import android.content.Context;

import pl.polsl.mmm.projectcomm.R;
import pl.polsl.mmm.projectcomm.objects.LightSource;
import pl.polsl.mmm.projectcomm.util.Geometry;

import static android.opengl.GLES20.GL_TEXTURE0;
import static android.opengl.GLES20.GL_TEXTURE_2D;
import static android.opengl.GLES20.glActiveTexture;
import static android.opengl.GLES20.glBindTexture;
import static android.opengl.GLES20.glGetAttribLocation;
import static android.opengl.GLES20.glGetUniformLocation;
import static android.opengl.GLES20.glUniform1i;
import static android.opengl.GLES20.glUniform3f;
import static android.opengl.GLES20.glUniform4f;
import static android.opengl.GLES20.glUniformMatrix4fv;
import static android.opengl.GLES20.glValidateProgram;

public class TextureLightShaderProgram extends ShaderProgram{
    //uniforms
    private final int uMatrixLocation;

    private final int uViewMatrixLocation;
    private final int uTextureUnitLocation;
    //private final int uColorLocation; //tylko debug
    //attributes
    private final int aPositionLocation;
    private final int aTextureCoordinatesLocation;
    private final int aNormalsLocation;

    //światło
    private final int uLightPos;
    private final int uLightAmb;
    private final int uLightDiff;
    private final int uLightSpec;

    public TextureLightShaderProgram(Context context) {
        super(context, R.raw.texture_light_vertex_shader, R.raw.texture_light_fragment_shader);
        //Retrieve uniform locations for the shader program.
        uMatrixLocation = glGetUniformLocation(program, U_MATRIX);
        uViewMatrixLocation = glGetUniformLocation(program, U_VIEW_MATRIX);
        uTextureUnitLocation = glGetUniformLocation(program, U_TEXTURE_UNIT);
        //kolor debug
        //uColorLocation = glGetUniformLocation(program, U_COLOR);

        //Retrieve attribute locations for the shader program.
        aPositionLocation = glGetAttribLocation(program, A_POSITION);
        aNormalsLocation = glGetAttribLocation(program, A_NORMAL);
        aTextureCoordinatesLocation = glGetAttribLocation(program, A_TEXTURE_COORDINATES);

        //światło
        uLightPos = glGetUniformLocation(program, U_LIGHT_POSITION);
        uLightAmb = glGetUniformLocation(program, U_LIGHT_AMBIENT);
        uLightDiff = glGetUniformLocation(program, U_LIGHT_DIFFUSE);
        uLightSpec = glGetUniformLocation(program, U_LIGHT_SPECULAR);

        glValidateProgram(program);
    }

    public void setMatrices(float[] matrix,float[] viewMatrix) {
        //przekazanie macierzy do programu shadera
        glUniformMatrix4fv(uMatrixLocation, 1, false, matrix, 0);
        glUniformMatrix4fv(uViewMatrixLocation, 1, false, viewMatrix, 0);

    }
    public void setTexture(int textureId) {
        // aktywna tekstura: tekstura 0
        glActiveTexture(GL_TEXTURE0);
        // powiązanie naszej tekstury z wybrana wczesniej aktywną jednostką
        glBindTexture(GL_TEXTURE_2D, textureId);
        // wpisanie tekstury do zmiennej uniform shadera
        glUniform1i(uTextureUnitLocation,0);
    }


    public void setLight(LightSource light) {
        //przekazanie pozycji źródła światła
        glUniform3f(uLightPos,
                light.getPosition().x, light.getPosition().y, light.getPosition().z);
        glUniform4f(uLightAmb,
                light.getAmbient().getR(),light.getAmbient().getG(),light.getAmbient().getB(),light.getAmbient().getA());
        glUniform4f(uLightDiff,
                light.getDiffuse().getR(),light.getDiffuse().getG(),light.getDiffuse().getB(),light.getDiffuse().getA());
        glUniform4f(uLightSpec,
                light.getSpecular().getR(),light.getSpecular().getG(),light.getSpecular().getB(),light.getSpecular().getA());
    }

    public int getPositionAttributeLocation() {
        return aPositionLocation;
    }

    public int getTextureCoordinatesAttributeLocation() {
        return aTextureCoordinatesLocation;
    }

    public int getNormalsAttributeLocation() {
        return aNormalsLocation;
    }


}

